# @Author: dpf
# @Date  : 2024/12/02/16:31
import sys

from PyQt6 import QtCore, QtGui, QtWidgets
from PyQt6.QtWidgets import QWidget, QApplication, QMessageBox

from dao import bookTypeDao
from entity.BookTypeModel import BookType


class Ui_Form(QWidget):

    def __init__(self):
        super(Ui_Form, self).__init__()
        self.setWindowFlag(QtCore.Qt.WindowType.MSWindowsFixedSizeDialogHint)
        self.setupUi(self)

    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(473, 397)
        self.formLayoutWidget = QtWidgets.QWidget(parent=Form)
        self.formLayoutWidget.setGeometry(QtCore.QRect(50, 40, 351, 251))
        self.formLayoutWidget.setObjectName("formLayoutWidget")
        self.formLayout = QtWidgets.QFormLayout(self.formLayoutWidget)
        self.formLayout.setContentsMargins(10, 10, 10, 10)
        self.formLayout.setVerticalSpacing(20)
        self.formLayout.setObjectName("formLayout")
        self.label = QtWidgets.QLabel(parent=self.formLayoutWidget)
        self.label.setObjectName("label")
        self.formLayout.setWidget(0, QtWidgets.QFormLayout.ItemRole.LabelRole, self.label)
        self.bookTypeInput = QtWidgets.QLineEdit(parent=self.formLayoutWidget)
        self.bookTypeInput.setObjectName("bookTypeInput")
        self.formLayout.setWidget(0, QtWidgets.QFormLayout.ItemRole.FieldRole, self.bookTypeInput)
        self.label_2 = QtWidgets.QLabel(parent=self.formLayoutWidget)
        self.label_2.setObjectName("label_2")
        self.formLayout.setWidget(1, QtWidgets.QFormLayout.ItemRole.LabelRole, self.label_2)
        self.bookTypeDescInput = QtWidgets.QPlainTextEdit(parent=self.formLayoutWidget)
        self.bookTypeDescInput.setObjectName("bookTypeDescInput")
        self.formLayout.setWidget(1, QtWidgets.QFormLayout.ItemRole.FieldRole, self.bookTypeDescInput)
        self.addBookTypeBtn = QtWidgets.QPushButton(parent=Form)
        self.addBookTypeBtn.setGeometry(QtCore.QRect(60, 320, 71, 31))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.addBookTypeBtn.setFont(font)
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap("../images/add.png"), QtGui.QIcon.Mode.Normal, QtGui.QIcon.State.Off)
        self.addBookTypeBtn.setIcon(icon)
        self.addBookTypeBtn.setIconSize(QtCore.QSize(18, 18))
        self.addBookTypeBtn.setObjectName("addBookTypeBtn")
        # 绑定图书类别添加按钮的槽函数
        self.addBookTypeBtn.clicked.connect(self.add)

        self.resetBoolTypeBtn = QtWidgets.QPushButton(parent=Form)
        self.resetBoolTypeBtn.setGeometry(QtCore.QRect(190, 320, 71, 31))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.resetBoolTypeBtn.setFont(font)
        icon1 = QtGui.QIcon()
        icon1.addPixmap(QtGui.QPixmap("../images/reset_btn.png"), QtGui.QIcon.Mode.Normal, QtGui.QIcon.State.Off)
        self.resetBoolTypeBtn.setIcon(icon1)
        self.resetBoolTypeBtn.setIconSize(QtCore.QSize(18, 18))
        self.resetBoolTypeBtn.setObjectName("resetBoolTypeBtn")
        # 绑定图书类别充值按钮的槽函数
        self.resetBoolTypeBtn.clicked.connect(self.reset)

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "图书类别添加"))
        self.label.setText(_translate("Form", "图书类别名称"))
        self.label_2.setText(_translate("Form", "图书类别描述"))
        self.addBookTypeBtn.setText(_translate("Form", "添加"))
        self.resetBoolTypeBtn.setText(_translate("Form", "重置"))

    def reset(self):
        self.bookTypeInput.clear()
        self.bookTypeDescInput.clear()

    def add(self):
        bookTypeName = self.bookTypeInput.text()
        bookTypeDesc = self.bookTypeDescInput.toPlainText()

        if bookTypeName.strip() == "":
            QMessageBox.warning(None, '系统提示', '图书类别名称不能为空')
        else:
            bookType = BookType(bookTypeName, bookTypeDesc)
            if bookTypeDao.add(bookType) > 0:
                QMessageBox.information(None, '系统提示', '添加成功')
                self.reset()
            else:
                QMessageBox.warning(None, '系统提示', '添加失败')


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ui = Ui_Form()
    ui.show()
    sys.exit(app.exec())
